﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;

namespace NotebookS2
{
    public partial class M_SALESMAN : RibbonForm
    {
        public M_SALESMAN()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }

        public void ShowSearch()
        {
            FND_SMN oShowSearch = new FND_SMN();

            oShowSearch.ConnectionString = this.ConnectionString;
            oShowSearch.ShowDialog();

            string sAcctCode = oShowSearch.AccountCode;
            if (sAcctCode != null)
            {
                txtCode.Text = sAcctCode;
                txtEmployeeID.Text = oShowSearch.EmployeeID;
                txtEmployeeName.Text = oShowSearch.AccountName;
                txtCode.Enabled = false;
                btnAdd.Text = "Update";
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AccountUnit oUnit = new AccountUnit();

            oUnit.Code = txtCode.Text;
            oUnit.EmpID = txtEmployeeID.Text;
            oUnit.AccountName = txtEmployeeName.Text;
            oUnit.AccountType = "Salesman";
            if (btnAdd.Text == "Add")
            {
                AccountManager oManager = new AccountManager();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();
                oManager.AddAccount(oUnit);
                oManager.Close();

                btnAdd.Text = "Update";
                txtCode.Enabled = false;
                MessageBox.Show("New salesman was successfully added to our database", "Salesman", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                AccountManager oManager = new AccountManager();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();
                oManager.UpdateAccount(oUnit);
                oManager.Close();
                
                MessageBox.Show("Successfully updated into the database.", "Salesman", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void txtEmployeeID_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FND_EMPLOYEE oSearch = new FND_EMPLOYEE();
            EmployeeManager oManger = new EmployeeManager();
            oSearch.ConnectionString = this.ConnectionString;
            oSearch.ShowDialog();

            string sEmpID = oSearch.EmployeeID;

            if (sEmpID != null)
            {
                txtEmployeeID.Text = sEmpID;
                txtEmployeeName.Text = oSearch.EmployeeFullName;
                oManger.ConnectionString = this.ConnectionString;
                oManger.Open();
                DataTable _Table = oManger.GetEmployeeInfo(sEmpID);
                oManger.Close();

                DataRow oRow = _Table.Rows[0];

                
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void M_SALESMAN_Load(object sender, EventArgs e)
        {

        }

        private void txtEmployeeID_EditValueChanged(object sender, EventArgs e)
        {

        }
    }
}
